package cn.zwx.business.order.remote;

import cn.zwx.business.order.api.req.RpcRequest;

import java.io.*;
import java.net.Socket;

/**
 * @author zhangwenxue
 */
public class ProcessorHandler extends Thread {

    private Socket socket;


    public ProcessorHandler(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try(ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
            ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());) {
            Object object = inputStream.readObject();
            if (object instanceof RpcRequest){
                // 读取客户端传过来的数据
                RpcRequest request = (RpcRequest) object;
                // 进行反射调用
                Mediator instance = Mediator.getInstance();
                Object invoke = instance.process(request);
                objectOutputStream.writeObject(invoke);
                objectOutputStream.flush();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
